import { get } from '../../utils/request'
const actions = {
  // 查询所有资讯分类信息
  findAllCategory(context) {
    // 异步操作
    get("/category/findAll").then(response => {
      context.commit('SET_CATEGORY', response.data)
    })
  },
  // 查询所有资讯信息
  findAllInfo(context,list) {
    get('/article/pageQuery',list).then(response => {
      let status=new Set()
      for (let item of response.data.list) {
        if (item.status === "审核通过") {
          item.type = "success";
        } else if (item.status === "审核不通过") {
          item.type = "danger";
        }else if (item.status === "推荐") {
          item.type = "warning";
        } else if (item.status === "未审核"){
          item.type = "info";
        }else{
          item.type = null;
        }
        status.add(item.status)
      }
      context.commit('SET_INFO', response.data.list)
      context.commit('SET_TOTAL', response.data.total)
      context.commit('SET_STATUS', status)
    })
  },
  // 获取用户信息
  getUserInfo(context,list){
    get('/user/info',list).then(response=>{
      context.commit('SET_USERINFO', response.data)
    })
  }

}
const mutations = {
  //修改state中的值
  SET_CATEGORY(state, response) {
    state.category = response
  },
  SET_INFO(state, response) {
    state.info = response
  },
  SET_TOTAL(state, response) {
    state.total = response
  },
  SET_STATUS(state, response) {
    state.status = response
  },
  SET_USERINFO(state, response) {
    state.userInfo = response
  },
}
const state = {
  category: null,
  info: null,
  total: null,
  status:[],
  userInfo:{},
}

export default {
  namespaced: true,
  state,
  actions,
  mutations,
}